package com.example.linyiquan.app.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.linyiquan.app.R;
import com.example.linyiquan.app.beam.AppBeam;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by linyiquan on 17-4-15.
 */

public class GridAdapter extends BaseAdapter {

    List<AppBeam> items = new ArrayList<>();


    /**
     * @param context
     * @param items
     */
    public GridAdapter(Context context, List<AppBeam> items) {
        this.items = items;
        //添加按钮
        this.items.add(new AppBeam("add",
                context.getResources().getDrawable(R.drawable.button)
                , ""));
        //删除按钮
        this.items.add(new AppBeam("remove",
                context.getResources().getDrawable(R.drawable.button)
                , ""));

    }

    /**
     * 获取视图长度
     *
     * @return
     */
    @Override
    public int getCount() {
        return items.size();
    }

    /**
     * 获取视图
     *
     * @param position
     * @return
     */
    @Override
    public Object getItem(int position) {
        return items.get(position);
    }

    /**
     * 获取视图索引
     *
     * @param position
     * @return
     */
    @Override
    public long getItemId(int position) {
        return position;
    }

    /**
     * 获取视图
     *
     * @param position
     * @param convertView
     * @param parent
     * @return
     */
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        AppBeam item = items.get(position);
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item, null);
        ImageView imageView = (ImageView) view.findViewById(R.id.img_item);
        TextView title = (TextView) view.findViewById(R.id.tv_item);
        imageView.setImageDrawable(item.getIcon());
        title.setText(item.getAppName());
        return view;
    }
}
